from django.db import models
from django.contrib.auth.models import AbstractUser
from utils.model import BaseModel
from utils.common import check_vip_exp

class User(AbstractUser):
    mobile = models.CharField(max_length=11, unique=True, verbose_name="用户手机号")
    icon = models.ImageField(upload_to='icon', default='icon/default.png', verbose_name="用户头像")
    vip_type = models.IntegerField(choices=[(0,"普通会员"),(1,"黄金会员"),(2,"至尊会员")], default=0, verbose_name="用户会员类型")
    exp_date = models.DateTimeField(null=True, blank=True, verbose_name='会员过期时间')

    @property
    def get_vip_type(self):
        check_vip_exp(self)
        return self.get_vip_type_display()

    class Meta:
        db_table = 'db_user'
        # 后台显示的表名
        verbose_name_plural = '用户表'

    def __str__(self):
        return self.username


# 用户详情表
class User_Detail(BaseModel):
    nick_name = models.CharField(max_length=32, verbose_name='昵称')
    birthday = models.DateField(null=True, blank=True, verbose_name='生日')
    gender = models.CharField(max_length=6, choices=( ('0', '女'),('1', '男')), null=True, blank=True,
                              verbose_name='性别')
    desc = models.TextField(max_length=255, verbose_name="用户简介", blank=True, null=True)
    user = models.OneToOneField(to="user", db_constraint=False, on_delete=models.CASCADE, verbose_name="视频类别")

    @property
    def gender_name(self):
        return self.get_gender_display()

    @property
    def vip_type(self):
        check_vip_exp(self.user)
        return self.user.get_vip_type_display()

    @property
    def exp_time(self):
        if self.user.vip_type != 0:
            return self.user.exp_time.strftime("%Y-%m-%d %H:%M:%S")
        else:
            return None

    class Meta:
        db_table = 'db_user_detail'
        # 后台显示的表名
        verbose_name_plural = '用户详情表'

    def __str__(self):
        return self.nick_name